/***************************************************************************
 *   Copyright (C) 2009 by Jason Pullara                                   *
 *   lordkat@gmail.com                                                     *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
 ***************************************************************************/

#include "common.h"

using namespace std;

cTables::cTables() {
/*
	sStatusTable statusTableRace[] = {
		{RACE_NONE, 	"ERR",	"ERROR",			'x', 'x', 'x', 'x', 'x', 'x', 'x', 'x', 'x'},
		{RACE_GM,		"GM",	"Game Master",		'Z', 'Z', 'Z', 'Z', 'Z', 'Z', 'Z', 'Z', 'Z'},
		{RACE_HUME, 	"HUM",	"Hume",				'D', 'D', 'D', 'D', 'D', 'D', 'D', 'D', 'D'},
		{RACE_ELVAAN,	"ELV",	"Elvaan",			'C', 'E', 'B', 'E', 'C', 'F', 'F', 'B', 'D'},
		{RACE_TARUTARU,	"TAR",	"Tarutaru",			'G', 'A', 'F', 'D', 'E', 'C', 'A', 'E', 'D'},
		{RACE_MITHRA,	"MIT",	"Mithra",			'D', 'D', 'E', 'A', 'E', 'B', 'D', 'E', 'F'},
		{RACE_GALKA,	"GAL",	"Galka",			'A', 'G', 'C', 'D', 'A', 'E', 'E', 'D', 'F'},
		{RACE_BEAST,	"BST",	"Beast",			'A', 'G', 'B', 'C', 'B', 'G', 'G', 'G', 'G'},
		{RACE_LIZARD,	"LIZ",	"Lizard",			'B', 'G', 'C', 'B', 'A', 'B', 'G', 'G', 'G'},
		{RACE_VERMIN,	"VER",	"Vermin",			'C', 'D', 'D', 'C', 'C', 'A', 'D', 'D', 'G'},
		{RACE_PLANTOID,	"PLA",	"Plantoid",			'C', 'C', 'D', 'D', 'D', 'D', 'B', 'B', 'G'},
		{RACE_AQUAN,	"AQU",	"Aquan",			'B', 'G', 'B', 'B', 'B', 'D', 'D', 'D', 'G'},
		{RACE_AMORPH,	"AMO",	"Amorph",			'G', 'B', 'D', 'D', 'C', 'C', 'B', 'B', 'G'},
		{RACE_BIRD,		"BRD",	"Bird",				'B', 'D', 'C', 'A', 'A', 'A', 'G', 'G', 'G'},
		{RACE_UNDEAD,	"UND",	"Undead",			'A', 'A', 'A', 'C', 'E', 'E', 'G', 'G', 'G'},
		{RACE_ARCANA,	"ARC",	"Arcana",			'G', 'A', 'G', 'G', 'G', 'G', 'A', 'A', 'G'},
		{RACE_DRAGON,	"DRG",	"Dragon",			'A', 'A', 'A', 'D', 'A', 'A', 'C', 'C', 'G'},
		{RACE_DEMON,	"DMN",	"Demon",			'A', 'A', 'A', 'A', 'A', 'A', 'G', 'G', 'G'},
		{RACE_LUMINIAN,	"LAN",	"Luminian",			'A', 'A', 'B', 'C', 'E', 'E', 'A', 'A', 'A'},
		{RACE_LUMINION,	"LON",	"Luminion",			'A', 'A', 'E', 'E', 'E', 'E', 'A', 'A', 'A'},
		{RACE_EMPTY,	"EMP",	"Empty",			'C', 'A', 'B', 'E', 'G', 'G', 'B', 'B', 'G'},
		{RACE_ANTICA,	"ANT",	"Antica",			'G', 'A', 'A', 'C', 'C', 'C', 'A', 'G', 'G'},
		{RACE_BUGBEAR,	"BUG",	"Bugbear",			'A', 'G', 'D', 'C', 'D', 'C', 'B', 'B', 'G'},
		{RACE_GIGAS,	"GIG",	"Gigas",			'A', 'G', 'A', 'A', 'G', 'G', 'G', 'G', 'G'},
		{RACE_GOBLIN,	"GOB",	"Goblin",			'C', 'C', 'C', 'C', 'C', 'C', 'C', 'C', 'C'},
		{RACE_LAMIAE,	"LAM",	"Lamiae",			'B', 'B', 'B', 'B', 'G', 'B', 'G', 'G', 'G'},
		{RACE_MAMOOLJA,	"MAM",	"Mamool Ja",		'B', 'B', 'C', 'D', 'C', 'A', 'B', 'C', 'G'},
		{RACE_MOBLIN,	"MOB",	"Moblin",			'A', 'C', 'A', 'C', 'C', 'A', 'D', 'D', 'G'},
		{RACE_ORC,		"ORC",	"Orc",				'C', 'G', 'B', 'B', 'D', 'C', 'F', 'F', 'G'},
		{RACE_POROGGO,	"POR",	"Poroggo",			'C', 'A', 'F', 'F', 'A', 'D', 'A', 'A', 'A'},
		{RACE_QIQIRN,	"QIQ",	"Qiqirn",			'C', 'E', 'D', 'A', 'A', 'A', 'C', 'C', 'G'},
		{RACE_QUADAV,	"QDV",	"Quadav",			'B', 'B', 'B', 'B', 'B', 'B', 'D', 'D', 'G'},
		{RACE_SAHAGIN,	"SHG",	"Sahagin",			'C', 'C', 'B', 'C', 'D', 'B', 'B', 'B', 'D'},
		{RACE_TONBERRY,	"TON",	"Tonberry",			'D', 'A', 'D', 'B', 'A', 'A', 'C', 'C', 'G'},
		{RACE_TROLL,	"TRL",	"Troll",			'A', 'F', 'A', 'D', 'F', 'D', 'C', 'C', 'G'},
		{RACE_YAGUDO,	"YAG",	"Yagudo",			'B', 'C', 'B', 'B', 'G', 'A', 'C', 'C', 'G'},
		{RACE_GENERIC,	"GEN",	"Generic",			'G', 'G', 'G', 'G', 'G', 'G', 'G', 'G', 'G'}
	};

	for (int i = 0; i < RACE_GENERIC; i++) {
		this->m_statusTableRace[i] = statusTableRace[i];
	}

	sStatusTable statusTableJob[] = {
		{JOB_NONE,			"ERR",	"ERROR",		'x', 'x', 'x', 'x', 'x', 'x', 'x', 'x', 'x'},
		{JOB_GM,			"GM ",	"Game Master",	'Z', 'Z', 'Z', 'Z', 'Z', 'Z', 'Z', 'Z', 'Z'},
		{JOB_WARRIOR,		"WAR",	"Warrior",		'B', 'x', 'A', 'C', 'D', 'C', 'F', 'F', 'E'},
		{JOB_MONK,			"MNK",	"Monk",			'A', 'x', 'C', 'B', 'A', 'F', 'G', 'D', 'E'},
		{JOB_MAGE_WHITE,	"WHM",	"White Mage",	'E', 'C', 'D', 'F', 'D', 'E', 'E', 'A', 'C'},
		{JOB_MAGE_BLACK,	"BLM",	"Black Mage",	'F', 'B', 'F', 'C', 'F', 'C', 'A', 'E', 'D'},
		{JOB_MAGE_RED,		"RDM",	"Red Mage",		'D', 'D', 'D', 'D', 'E', 'E', 'C', 'C', 'D'},
		{JOB_THIEF,			"THF",	"Thief",		'D', 'x', 'D', 'A', 'D', 'B', 'C', 'G', 'G'},
		{JOB_PALADIN,		"PAL",	"Paladin",		'C', 'F', 'B', 'E', 'A', 'G', 'G', 'C', 'C'},
		{JOB_DARK_KNIGHT,	"DRK",	"Dark Knight",	'C', 'F', 'A', 'C', 'C', 'D', 'C', 'G', 'G'},
		{JOB_BEASTMASTER,	"BST",	"Beast Master",	'C', 'x', 'D', 'C', 'D', 'F', 'E', 'E', 'A'},
		{JOB_BARD,			"BRD",	"Bard",			'D', 'x', 'D', 'D', 'D', 'F', 'D', 'D', 'B'},
		{JOB_RANGER,		"RNG",	"Ranger",		'E', 'x', 'E', 'D', 'D', 'A', 'E', 'D', 'E'},
		{JOB_NINJA,			"NIN",	"Ninja",		'D', 'x', 'C', 'B', 'C', 'B', 'D', 'G', 'F'},
		{JOB_SUMMONER,		"SMN",	"Summoner",		'G', 'A', 'G', 'D', 'G', 'D', 'B', 'C', 'C'},
		{JOB_SAMURAI,		"SAM",	"Samurai",		'B', 'x', 'C', 'C', 'C', 'D', 'E', 'E', 'D'},
		{JOB_DRAGOON,		"DRG",	"Dragoon",		'C', 'x', 'C', 'E', 'C', 'E', 'F', 'E', 'C'},
		{JOB_MAGE_BLUE,		"BLU",	"Blue Mage",	'E', 'E', 'E', 'D', 'E', 'E', 'E', 'E', 'E'},
		{JOB_PUPPETMASTER,	"PUP",	"Puppet Mater",	'C', 'x', 'E', 'B', 'D', 'C', 'E', 'F', 'C'},
		{JOB_CORSAIR,		"COR",	"Corsair",		'D', 'x', 'E', 'C', 'E', 'C', 'C', 'E', 'E'},
		{JOB_DANCER,		"DNC",	"Dancer",		'C', 'x', 'D', 'C', 'E', 'C', 'F', 'F', 'B'},
		{JOB_SCHOLAR,		"SCH",	"Scholar",		'G', 'C', 'G', 'C', 'E', 'D', 'C', 'D', 'C'},
		{JOB_GENERIC,		"GEN",	"Generic",		'G', 'G', 'G', 'G', 'G', 'G', 'G', 'G', 'G'}
	};

	for (int i = 0; i < JOB_GENERIC; i++) {
		this->m_statusTableJob[i] = statusTableJob[i];
	}
*/
	sScaleTable tableScale[] = {
		{'A',	19,	9,	1,	16,	6,		0,	5,	0.5},
		{'B',	17,	8,	1,	14,	5,		0,	4,	0.45},
		{'C',	16,	7,	1,	12,	4,		0,	4,	0.4},
		{'D',	14,	6,	0,	10,	3,		0,	3,	0.35},
		{'E',	13,	5,	0,	8,	2,		0,	3,	0.3},
		{'F',	11,	4,	0,	6,	1,		0,	2,	0.25},
		{'G',	10,	3,	0,	4,	0.5,	0,	2,	0.2},
		{'H',	8,	2,	0,	2,	0.25,	0,	1,	0.15},
		{'I',	7,	1,	0,	0,	0.1,	0,	0,	0.1},
		{'J',	5,	0,	0,	0,	0,		0,	0,	0},
		{'Z',	40,	20,	10,	40,	20,		0,	45,	0.97},
		{'x',	0,	0,	0,	0,	0,		0,	0,	0},
		{'~',	0,	0,	0,	0,	0,		0,	0,	0}
	};

	for (int i = 0; i < 13; i++) {
		this->m_tableScale[i] = tableScale[i];
	}

	sExperienceTable experienceTable[] = {
		{1,		250},
		{20,	500},
		{30,	750},
		{50,	1000},
		{70,	1200},
		{80,	1500},
		{90,	2000}
	};

	for (int i = 0; experienceTable[i].uiLevel <= 80; i++)
		this->m_experienceTable[i] = experienceTable[i];

	this->m_semTableScaleLock = SDL_CreateSemaphore(1);
	this->m_semTableLevelLock = SDL_CreateSemaphore(1);
}

cTables::~cTables() {
	SDL_DestroySemaphore(this->m_semTableScaleLock);
	SDL_DestroySemaphore(this->m_semTableLevelLock);
}

sScaleTable cTables::getScaleTable(char cAttribute) {
	sScaleTable scaleTemp;
	SDL_SemWait(this->m_semTableScaleLock);
	for (int i = 0; this->m_tableScale[i].cID != '~'; i++) {
		if (this->m_tableScale[i].cID == cAttribute || this->m_tableScale[i].cID == '~') {
			scaleTemp = this->m_tableScale[i];
			break;
		}
	}
	SDL_SemPost(this->m_semTableScaleLock);
	return scaleTemp;
}

unsigned long cTables::getLevelScale(unsigned int uiLevel) {
	unsigned int uiScale = 0;
	SDL_SemWait(this->m_semTableLevelLock);
	for (int i = 0; this->m_experienceTable[i].uiLevel <= 80; i++) {
		if (uiLevel < this->m_experienceTable[i].uiLevel)
			break;
		uiScale = this->m_experienceTable[i].uiScale;
	}
	SDL_SemPost(this->m_semTableLevelLock);
	return uiScale;
}
